package com.ews.onlineexchange.controller.assess;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.assess.AssessRecord;
import com.ews.onlineexchange.model.assess.Dict;
import com.ews.onlineexchange.service.EntityQuery;

@Model
public class AssessRecordList extends EntityQuery<AssessRecord> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String serviceSupplierName;
	private String dictId;
	
	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<AssessRecord> entity) {
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		restrictions.add( cb.equal( entity.get( "delFlag" ), DataEntity.DEL_FLAG_NORMAL ) );
		restrictions.add( cb.isFalse( entity.get( "override" ) ) );
		restrictions.add( cb.isFalse( entity.join( "dict" ).join( "assessClass" ).get( "allowSupplierApply" ) ) );
		if ( StringUtils.isNotBlank( serviceSupplierName ) ) {
			Join<AssessRecord, ServiceSupplier> supplier = entity.join( "serviceSupplier" );
			restrictions.add( cb.like( supplier.get( "cnname" ), getStringRestriction( serviceSupplierName ) ) );
		}
		if ( StringUtils.isNotBlank( dictId ) ) {
			Join<AssessRecord, Dict> dict = entity.join( "dict" );
			restrictions.add( cb.equal( dict.get( "id" ), dictId ) );
		}
		return restrictions;
	}

	public AssessRecordList() {
//		setMaxResults(null);
		setOrder("intStatus ASC, assessDate DESC, id ASC");
	}

	public String getServiceSupplierName() {
		return serviceSupplierName;
	}

	public void setServiceSupplierName(String serviceSupplierName) {
		this.serviceSupplierName = serviceSupplierName;
	}

	public String getDictId() {
		return dictId;
	}

	public void setDictId(String dictId) {
		this.dictId = dictId;
	}

}
